package com.qx.grainmall.product.service.impl;

import com.qx.grainmall.product.dao.*;
import com.qx.grainmall.product.entity.SkuImagesEntity;
import com.qx.grainmall.product.entity.SpuInfoDescEntity;
import com.qx.grainmall.product.entity.SpuInfoEntity;
import com.qx.grainmall.product.vo.ItemSaleAttrVo;
import com.qx.grainmall.product.vo.SkuItemVo;
import com.qx.grainmall.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qx.common.utils.PageUtils;
import com.qx.common.utils.Query;

import com.qx.grainmall.product.entity.SkuInfoEntity;
import com.qx.grainmall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuInfoDao skuInfoDao;

    @Autowired
    private SkuImagesDao skuImagesDao;

    @Autowired
    private SkuSaleAttrValueDao skuSaleAttrValueDao;

    @Autowired
    private SpuInfoDao spuInfoDao;

    @Autowired
    private SpuInfoDescDao spuInfoDescDao;

    @Autowired
    private ProductAttrValueDao productAttrValueDao;

//    @Autowired
//    private ThreadPoolExecutor threadPoolExecutor;// 线程池
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SkuItemVo item(Long skuId) throws IOException, ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
        //1.sku基本信息
//        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfo = skuInfoDao.selectById(skuId);
            skuItemVo.setSkuInfo(skuInfo);
//            return skuInfo;
//        }, threadPoolExecutor);
        //2.sku所有图片信息
//        CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> images = skuImagesDao.selectList(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
            skuItemVo.setImages(images);
//        },threadPoolExecutor);

        //3.spu销售属性信息
//        CompletableFuture<Void> saleAttrFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            List<ItemSaleAttrVo> saleAttr = skuSaleAttrValueDao.getSaleAttr(skuInfo.getSpuId());
            skuItemVo.setSaleAttr(saleAttr);
//        }, threadPoolExecutor);

        //4.获取spu介绍
//        CompletableFuture<Void> spuInfoFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            SpuInfoEntity spuInfoEntity = spuInfoDao.selectById(skuInfo.getSpuId());
            skuItemVo.setSpuInfoEntity(spuInfoEntity);
//        }, threadPoolExecutor);

        //5.spu的所有图片
//        CompletableFuture<Void> descFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            SpuInfoDescEntity spuInfoDescEntity= spuInfoDescDao.selectById(skuInfo.getSpuId());
            skuItemVo.setSpuInfoDescEntity(spuInfoDescEntity);
//        }, threadPoolExecutor);

        //6.spu对应的规格参数信息
//        CompletableFuture<Void> groupAttrsFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            List<SpuItemAttrGroupVo> groupAttrs =productAttrValueDao.getGroupAttrs(skuInfo.getSpuId());
            skuItemVo.setGroupAttrs(groupAttrs);
//        }, threadPoolExecutor);

        //等待所有执行结束 get()
//        CompletableFuture.allOf(skuInfoFuture,imagesFuture,saleAttrFuture,spuInfoFuture,descFuture,groupAttrsFuture).get();

        return skuItemVo;
    }

    @Override
    public List<String> stringlist(Long skuId) {
        return skuInfoDao.stringlist(skuId);
    }

}